package com.ruoyi.web.controller.team;

import com.ruoyi.RDTeam.domain.MemberInfo;
import com.ruoyi.RDTeam.service.IMemberInfoService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 研发团队成员展示Controller
 *
 * @author ruoyi
 * @date 2024-12-03
 */
@Anonymous
@RestController
@RequestMapping("/RDTeam/info")
public class MemberInfoController extends BaseController
{
    @Autowired
    private IMemberInfoService memberInfoService;



    /**
     * 查询研发团队成员展示列表
     */

//    @PreAuthorize("@ss.hasPermi('Team:info:list')")
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(MemberInfo memberInfo)
    {
        startPage();
        List<MemberInfo> list = memberInfoService.selectMemberInfoList(memberInfo);
        return getDataTable(list);
    }

    /**
     *  模糊查询
     */
    @Anonymous
    @GetMapping("/search")
    public List<MemberInfo> searchList(@RequestParam(required = false) String name,
                                       @RequestParam(required = false) String major,
                                       @RequestParam(required = false) String expertise) {
        return memberInfoService.searchList(name, major, expertise);
    }
    /**
     * 导出研发团队成员展示列表
     */
//    @PreAuthorize("@ss.hasPermi('RDTeam:info:export')")
    @Log(title = "研发团队成员展示", businessType = BusinessType.EXPORT)
    @Anonymous
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberInfo memberInfo)
    {
        List<MemberInfo> list = memberInfoService.selectMemberInfoList(memberInfo);
        ExcelUtil<MemberInfo> util = new ExcelUtil<MemberInfo>(MemberInfo.class);
        util.exportExcel(response, list, "研发团队成员展示数据");
    }

    /**
     * 获取研发团队成员展示详细信息
     */
//    @PreAuthorize("@ss.hasPermi('RDTeam:info:query')")
    @GetMapping(value = "/{memberId}")
    @Anonymous
    public AjaxResult getInfo(@PathVariable("memberId") String memberId)
    {
        return success(memberInfoService.selectMemberInfoByMemberId(memberId));
    }

    /**
     * 新增研发团队成员展示
     */
//    @PreAuthorize("@ss.hasPermi('RDTeam:info:add')")
    @Log(title = "研发团队成员展示", businessType = BusinessType.INSERT)
    @Anonymous
    @PostMapping
    public AjaxResult add(@RequestParam("memberId") String memberId,
                          @RequestParam("name") String name,
                          @RequestParam("pictureUrl") String pictureUrl,
                          @RequestParam("major") String major,
                          @RequestParam("joinTime") String joinTime,
                          @RequestParam("expertise") String expertise,
                          @RequestParam("employmentCompany") String employmentCompany,
                          @RequestParam("phone") String phone,
                          @RequestParam("isOnDuty") Long isOnDuty,
                          @RequestParam("avatarFile") MultipartFile avatarFile) {
        // 将文件设置到 memberInfo 对象中
        // 创建 MemberInfo 对象并设置数据
        MemberInfo memberInfo = new MemberInfo();
        memberInfo.setMemberId(memberId);
        memberInfo.setName(name);
        memberInfo.setMajor(major);
        memberInfo.setJoinTime(joinTime);
        memberInfo.setExpertise(expertise);
        memberInfo.setEmploymentCompany(employmentCompany);
        memberInfo.setPhone(phone);
        memberInfo.setPictureUrl(pictureUrl);
        memberInfo.setIsOnDuty(isOnDuty);
        // 将文件设置到 memberInfo 对象中
        memberInfo.setAvatarFile(avatarFile);
        // 调用服务层保存数据
        return toAjax(memberInfoService.insertMemberInfo(memberInfo));
    }

    /**
     * 修改研发团队成员展示
     */
//    @PreAuthorize("@ss.hasPermi('RDTeam:info:edit')")
    @Anonymous
    @Log(title = "研发团队成员展示", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestParam("memberId") String memberId,
                           @RequestParam("name") String name,
                           @RequestParam("pictureUrl") String pictureUrl,
                           @RequestParam("major") String major,
                           @RequestParam("joinTime") String joinTime,
                           @RequestParam("expertise") String expertise,
                           @RequestParam("employmentCompany") String employmentCompany,
                           @RequestParam("phone") String phone,
                          @RequestParam("isOnDuty") Long isOnDuty,
                           @RequestParam(value = "avatarFile", required = false) MultipartFile avatarFile ) {
        // 创建 MemberInfo 对象并设置数据
        MemberInfo memberInfo = new MemberInfo();
        memberInfo.setMemberId(memberId);
        memberInfo.setName(name);
        memberInfo.setMajor(major);
        memberInfo.setJoinTime(joinTime);
        memberInfo.setExpertise(expertise);
        memberInfo.setEmploymentCompany(employmentCompany);
        memberInfo.setPhone(phone);
        memberInfo.setPictureUrl(pictureUrl);
        memberInfo.setIsOnDuty(isOnDuty);
        // 将文件设置到 memberInfo 对象中
//        if(avatarFile){
            memberInfo.setAvatarFile(avatarFile);
//        }


        // 调用服务层更新数据
        return toAjax(memberInfoService.updateMemberInfo(memberInfo));
    }

    /**
     * 删除研发团队成员展示
     */
//    @PreAuthorize("@ss.hasPermi('RDTeam:info:remove')")
    @Anonymous
    @Log(title = "研发团队成员展示", businessType = BusinessType.DELETE)
    @DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable String[] memberIds)
    {
        return toAjax(memberInfoService.deleteMemberInfoByMemberIds(memberIds));
    }
}
